<?php
declare (strict_types = 1);

namespace app\command;

use app\common\model\Article;
use app\common\model\GzhAccount;
use app\common\model\GzhTitle;
use app\opengzh\service\OpenPlatformService;
use think\console\Command;
use think\console\Input;

use think\console\Output;

class Cmdcaogao extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('Cmdcaogao')
            ->setDescription('the Cmdcaogao command');
    }

    protected function execute(Input $input, Output $output)
    {

        //公众号生成的草稿单日限制次数
        $day_caogao_limit = 10;
        //每次获取标题数
        $title_count = 3;
    	// 指令输出 自动生成草稿
        $titleModel = new GzhTitle();
        $title_list = $titleModel
            ->where('is_article',2)
            ->where('is_caogao',1)
            ->order('id','asc')
            ->limit($title_count)
            ->select()
            ->toArray();
        if(sizeof($title_list)>0){
            //判断日期
            $today = date('Y-m-d',time());
            $accountModel = new GzhAccount();
            $account_info= $accountModel->where('last_caogao_day','<>',$today)
                ->where('is_use',2)
                ->order('id','asc')
                ->findOrEmpty();
            //判断有没有今天还没生成过草稿的公众号
            if(!$account_info->isEmpty()){
                //有就进去生成
                $account_info = $account_info->toArray();
                $opfService =  new OpenPlatformService();
                $accountModel->where('id',$account_info['id'])->save([
                   'last_caogao_day'=>$today,
                   'day_caogao_count'=>0,
                ]);
                $articleModel = new Article();
                foreach ($title_list as $v){
                    $content = $articleModel->where('title_id',$v['id'])->value('content2');
                    $res = $opfService->draft($account_info['appid'],$content,$v);
                    if($res['status']){
                        $accountModel->where('id',$account_info['id'])->inc('day_caogao_count')->update();
                        $accountModel->where('id',$account_info['id'])->inc('caogao_count')->update();
                        var_dump('草稿添加成功:'.$account_info['appid']);
                    }
                }
            }else{
                //这里就是所有公众号今天都生成过草稿了，然后判断有没有公众号生成的草稿低于单日限制的

                $account_info = $accountModel->where('last_caogao_day',$today)
                    ->where('day_caogao_count','<',$day_caogao_limit)
                    ->where('is_use',2)
                    ->order('day_caogao_count','asc')
                    ->order('id','asc')
                    ->findOrEmpty();
                if(!$account_info->isEmpty()){
                    $account_info = $account_info->toArray();
                    $day_caogao_count = $account_info['day_caogao_count'];
                    $opfService =  new OpenPlatformService();
                    $articleModel = new Article();
                    foreach ($title_list as $v){
                        if($day_caogao_count>=$day_caogao_limit){
                            return;
                        }
                        $content = $articleModel->where('title_id',$v['id'])->value('content2');
                        $res = $opfService->draft($account_info['appid'],$content,$v);
                        if($res['status']){
                            $accountModel->where('id',$account_info['id'])->inc('day_caogao_count')->update();
                            $accountModel->where('id',$account_info['id'])->inc('caogao_count')->update();
                            $day_caogao_count++;
                            var_dump('草稿添加成功:'.$account_info['appid']);
                        }
                    }
                }else{
                    var_dump('当日草稿任务已完成');
                }
            }
        }else{
            var_dump('没有可生成草稿的标题');
        }

    	$output->writeln('Cmdcaogao - over');
    }
}
